<!DOCTYPE html>
<html>

	<head>
		<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="../../css/iuapmobile.um.css">
		<link rel="stylesheet" href="../../css/font-icons.css">
		<script src="../../js/summer.js"></script>
		<script src="../../js/jquery.min.js"></script>
		<script src="../../js/Frameworks/iuapmobile.frameworks.ui.js"></script>
	</head>

	<body>
		<div class="um-win" id="index" style="height: 100%;">
			<div class="um-header">
				<a href="#" class="um-back back">返回 </a>
				<h3>调整图片方向</h3>
			</div>

			<div class="um-content" style="text-align:center;margin: 0px auto;position:realtive; width:100%; height:100%;vertical-align:middle;display:table-cell;">
				<canvas id="canvas" style="border: 1px solid;display: none;"></canvas>
				<img id="img" style="vertical-align:middle;width: 80%;" src="" />
			</div>

			<div id="divCenter" align="center" style="width: 100%; position: absolute; z-index: 3;  top: 50%;">
				<i class="ti-arrow-left fl pl10" onclick="change(-90)"></i>
				<i class="ti-arrow-right fr pr10" onclick="change(90)"></i>
			</div>

			<div class="um-footer">
				<button class="btns btnOk">
					确定
				</button>
			</div>
		</div>
	</body>

</html>
<script>
	var datasa = {
		id: "", //编号
		base64: "", //base64
		pageId: "asdasd.dasdad.sadsad" //page ID
	};
	var thisBase64 = "";
	//转换后的数据
	var ol = 0;
	//当前角度，缓存
	var ola = 0;
	//当前度数

	summerready = function() {
		datasa = summer.pageParam;
		if(summer.pageParam.base64.indexOf("http") == 0) { //如果是网上的图要先下载下来
			changeLoadingTitle("正在下载图片");
			summer.download({
				"url": summer.pageParam.base64,
				"locate": "download/image",
				"filename": Date.parse(new Date()).toString() + ".jpg",
				"override": "true",
				"callback": "downloadCallBack()"
			});
		} else {
			thisBase64 = summer.pageParam.base64;
			load();
		}
	}

	function downloadCallBack(args) {		
		if(args.isfinish) {
			if(!summer.getStorage("adressXiaZai")){
				var filepath = args.filepath;
				summer.setStorage("adressXiaZai",filepath);
			}
			
			changeLoadingTitle();
			thisBase64 = args.savePath;
			datasa.base64 = args.savePath;
			load();
		}
	}

	function load() {
		change(0);
	}
	//转换
	function change(ind) {
		ola = (ol += ind) > 0 ? (ol % 360) : ((n = ol % 360 + 360) == 360 ? 0 : n);
			ola = ola || 360;
			//90 180 270 360
			if(ola == 360) {
				thisBase64 = datasa.base64;
				jz();
				return;
			}
			changeLoadingTitle("正在旋转...");
			summer.callService("UMDevice.rotaingImageView", {
				angle: ola,
				srcpath: datasa.base64.replace("file://", ""), //源地址
				savepath: Date.parse(new Date()).toString() + ".jpg", //储存地址
				callback: function(a) {
					if(!summer.getStorage("adressXuanZhuan")){
						var filepath = a.savepath.substr(0, a.savepath.lastIndexOf("/")) + "/";
						summer.setStorage("adressXuanZhuan",filepath);
					}
					changeLoadingTitle();
					thisBase64 = a.savepath;
					jz();
				}
			}, false);
	}

	//绑定后调整位置
	function jz() {
		var img = document.getElementById("img");
		img.src = thisBase64;
		$("#img").css("margin-top", ($(".um-content").height() - $("#img").height()) / 2);
		setTimeout(function() {
			$("#img").css("margin-top", ($(".um-content").height() - $("#img").height()) / 2);
		}, 100);
	}

	//确定
	$(".btnOk").click(function() {
		changeLoadingTitle("处理中");
		datasa.base64 = thisBase64;
		var vimg = JSON.stringify(datasa);
		summer.setAppStorage("pageParamImageBase64", vimg);
		summer.execScript({
			winId: datasa.pageId,
			script: "rotateBack()"
		});
		summer.closeWin();
	});
</script>
<script src="../../js/Myjs/Common.js"></script>